package com.suncaper.wcss.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.suncaper.wcss.common.PageDTO;
import com.suncaper.wcss.model.vo.BatchSettingVO;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.stereotype.Controller;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.suncaper.wcss.common.JsonResponse;
import com.suncaper.wcss.service.BatchSettingService;
import com.suncaper.wcss.model.domain.BatchSetting;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;


/**
 * 前端控制器
 *
 * @author lxp
 * @version v1.0
 * @since 2021-11-11
 */
@Controller
@RequestMapping("/batchSetting")
public class BatchSettingController {

    private final Logger logger = LoggerFactory.getLogger(BatchSettingController.class);

    @Autowired
    private BatchSettingService batchSettingService;

    /**
     * 描述：根据Id 查询
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse getById(@PathVariable("id") Long id) throws Exception {
        BatchSetting batchSetting = batchSettingService.getById(id);
        return JsonResponse.success(batchSetting);
    }

    /**
     * 描述：根据Id删除
     */
    @RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
    @ResponseBody
    public JsonResponse deleteById(@PathVariable("id") Long id) throws Exception {
        batchSettingService.removeById(id);
        return JsonResponse.success(null);
    }


    /**
     * 描述：根据Id 更新
     */
    @RequestMapping(value = "/updateById", method = RequestMethod.GET)
    @ResponseBody
    public JsonResponse updateBatchSetting(Long id) throws Exception {
        //这个返回值表示是否进行修改
        Boolean flag = batchSettingService.updateBatch(id);
        return JsonResponse.success(flag);
    }


    /**
     * 描述:创建BatchSetting
     */
    @RequestMapping(value = "", method = RequestMethod.POST)
    @ResponseBody
    public JsonResponse create(BatchSetting batchSetting) throws Exception {
        batchSettingService.insert(batchSetting);
        return JsonResponse.success(null);
    }

    /**
     * 查询所有批次
     */
    @GetMapping("/list")
    @ResponseBody
    public JsonResponse list(PageDTO pageDTO,BatchSetting batchSetting) {
        //设置页面大小为-1表示不分页
        Page<BatchSettingVO> page = batchSettingService.pageList(new Page<>(pageDTO.getPageNo(), pageDTO.getPageSize()),batchSetting);
        return JsonResponse.success(page);
    }
    /**
     * 修改批次
     */
    @PostMapping("/update")
    @ResponseBody
    public  JsonResponse updateBatch(BatchSetting batchSetting){
        batchSetting.setGmtModified(LocalDateTime.now());
        batchSettingService.updateById(batchSetting);
        return JsonResponse.success(Boolean.TRUE);
    }
    /**
     * 批量删除
     */
    @GetMapping("/deletes")
    @ResponseBody
    public JsonResponse deletes(@RequestParam("ids[]")List<Long> ids){
        batchSettingService.removeByIds(ids);
        return JsonResponse.success(null);
    }
    /**
     * 获取当前批次
     */
    @GetMapping("/curBatch")
    @ResponseBody
    public JsonResponse curBatch(){
        BatchSetting batchSetting =  batchSettingService.curBatch();
        return  JsonResponse.success(batchSetting);
    }

    /**
     * 验证当前时间是否为选衣阶段中
     */
    @GetMapping("/yanzheng")
    @ResponseBody
    public JsonResponse yanzheng(Long id){
      Boolean flag =  batchSettingService.yanzheng(id);
      return  JsonResponse.success(flag);
    }
}

